<?php

namespace App\Entities;

use App\Contracts\Services\Storage\StorageService;
use App\Models\Groupable;
use App\Support\Database\Eloquent\UuidModel;
use Carbon\Carbon;
use Illuminate\Container\Container;

class Prize extends UuidModel
{
    use Groupable;

    //奖品是否可用，0否、1是
    const IS_VALID_YES = 1;
    const IS_VALID_NO = 0;
    //奖品来源 1自建 2购买
    const BUILD_BY_CREATE = 1;
    const BUILD_BY_BUY = 2;
    //领取方式 0现场兑换 1 邮寄
    const SPOT_EXCHANGE = 0;
    const MAIL_POST = 1;
    //券码类型 0无 1统一码 2自定义csv
    const NO_VOUCHER = 0;
    const UNICODE_VOUCHER = 1;
    const CUSTOM_VOUCHER = 2;
    //奖品来源
    const RESOURCE_TYPES = [
        '1' => '自建奖品',
        '2' => '平台购买'
    ];

    protected $fillable = [
        'user_id',
        'type_code',
        'name',
        'expired_start',
        'expired_end',
        'stock_number',
        'tag',
        'background_image',
        'exchange_method',
        'exchange_address',
        'voucher_type',
        'voucher_code',
        'voucher_link',
        'exchange_url',
        'resource',
        'is_valid',
        'group_code',
        'extends',
    ];

    protected $guarded = ['created_at', 'updated_at'];

    public function type()
    {
        return $this->hasOne(PrizeType::class,  'code', 'type_code');
    }

    public function sponsors()
    {
        return $this->hasMany(Sponsor::class, 'prize_id', 'id');
    }

    public function activities()
    {
        return $this->belongsToMany('\App\Models\ActivityInfo\ActivityInfo', 'activity_prizes', 'prize_id', 'activity_id');
    }

    public function records()
    {
        return $this->hasMany('\App\Models\PrizeRecord\PrizeRecord', 'prize_id', 'id');
    }

    public function payments()
    {
        return $this->hasOne('\App\Models\PrizePayment\PrizePayment', 'prize_id', 'id');
    }

    public function scopeNotExpired()
    {
        $now = Carbon::now();

        return $this->where([
           ['expired_start', '<=', $now],
           ['expired_end', '>=', $now],
        ]);
    }
}
